package com.example.common.utils.spring;

import org.apache.commons.io.function.Uncheck;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.swing.*;

/**
 * Spring工具类，方便在非spring管理环境中获取bean
 */
@Component
public class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {
    /**
     * Spring应用上下文环境
     */
    private static ConfigurableListableBeanFactory beanFactory;

    /**
     * 应用上下文
     */
    private static ApplicationContext applicationContext;

    /**
     * @param configurableListableBeanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        SpringUtils.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }

    /**
     * 获取对象
     *
     * @param name
     * @param <T>
     * @return Object 一个用名字注册的bean实例
     * @throws BeansException
     */
    public static <T> T getBean(String name) throws BeansException {
        return (T) beanFactory.getBean(name);
    }

    /**
     * 获取类型为clazz的对象
     *
     * @param clazz
     * @param <T>
     * @return
     * @throws BeansException
     */
    public static <T> T getBean(Class<T> clazz) throws BeansException {
        return (T) beanFactory.getBean(clazz);
    }

    /**
     * 如果BeanFactory包含一个与所给名称匹配的bean定义，则返回true
     *
     * @param name
     * @return
     */
    public static boolean containsBean(String name) {
        return beanFactory.containsBean(name);
    }

    /**
     * 判断给定名字注册的bean是否为singleton，还是prototype模式的bean
     *
     * @param name
     * @return
     * @throws BeansException
     */
    public static boolean isSingleton(String name) throws BeansException {
        return beanFactory.isSingleton(name);
    }

    /**
     * 获取注册对象的类型
     *
     * @param name
     * @return
     * @throws BeansException
     */
    public static Class<?> getType(String name) throws BeansException {
        return beanFactory.getType(name);
    }

    /**
     * 获取注册对象的别名
     *
     * @param name
     * @return
     * @throws BeansException
     */
    public static String[] getAliases(String name) throws BeansException {
        return beanFactory.getAliases(name);
    }

    /**
     * 获取aop代理对象
     *
     * @param invoker
     * @param <T>
     * @return
     */
    public static <T> T getAopProxy(T invoker) {
        return (T) AopContext.currentProxy();
    }

    /**
     * 获取当前环境的活动配置文件,没有配置返回null
     *
     * @return 活动配置文件数组
     */
    public static String[] getActiveProfiles() {
        return applicationContext.getEnvironment().getActiveProfiles();
    }
}
